{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Simulation\n", "## Introduction\n", "Simulation is a decision support tool that provides deep insights of the behaviour of a system, by representing it through a mathematical model and using a computer to run the model and obtain a virtual representation of the system. Thus, the main concepts are:\n", "\n", "- **Simulation Model**: Set of mathematical rules (e.g. differential equations, state machines) that defines the behaviour of the system. Normally, the simulation model is formulated such that future system states are obtained from the current state, to model the system behaviour over time.\n", "\n", "- **Simulation**: We refer to the execution of the simulation model as simulation, that is, simulation consists of taking the model through state changes over time to obtain a (virtual) representation of its behaviour\n", "\n", "Simulation is used when there is no mathematical or analytical solution to a problem, or when the available methods are just too complex.\n", "From a practical point of view, simulation is also used when we want to experiment with the system behaviour before it is build, to get a better understanding of it, or just to visualize, demonstrate, or experiment with its behaviour. In some applications, it is not possible to perform such experiments with the real system, due to economic reasons (it is too expensive), security (it is too dangerous) or ethical reasons.\n", "Computers can run simulation models through state changes at much higher rates than the actual system behaviour. For instance, a computer may run a simulation of a model of the evolution of the earth climate through thousand of years in a matter of hours. Thus, simulation can also be used to save time. Finally, by running simulations before the real system we may also gain control over system conditions. \n", "\n", "## Simulation Model Types\n", "### System Dynamics \n", "System dynamic models use differential equations to model the system behaviour of the model. System Dynamics models are also known as Continuous Simulation models or Differential-Algebraic Equations (DAE) models. One of the main characteristics of these models is that the lack of individuality of the inner components of the system. A set of differential equations model the system behaviour as a hole, where its individual elements are not represented individually, but instead, they are grouped into entities like Queues or Stocks. The model just defines the casual relationships and flows between the stocks. \n", "\n", "For instance, a system dynamics model can be used to model the adoption of a product. \n", "In this example, all potential customers are modeled as a stock variable that depends on time $p_s(t)$. The simulation is initialised with an initial population of potential customers of $p_s(0)$. The actual customers on the other hand are modeled as another stock variable $c_s(t)$. Let us assume that there are no customers at the beginning of the simulation and that therefore $c_s(0) = 0$. The adoption rate $a$ represents the flow of customers between these two stocks, that is $\\frac{\\delta c_s(t)}{\\delta t} = a$ and $\\frac{\\delta c_s(t)}{\\delta t} = -a$. Thus, we can obtain the number of potential customers or customers at any given time t as: \n", "\n", "$c_s(t) = c_s(0) + a*t = a*t$\n", "\n", "$p_s(t) = p_s(0) - a*t$\n", "\n", "Note that in this model, individual customers are not represented. \n", " \n", "### Discrete Events\n", "In discrete events' simulation, the system is modeled as a flow chart that represents the system as a sequence of steps. Therefore, the blocks in the flow chart represent different steps in the overall system. The flowchart blocks are modeled as stochastic processes that drive the simulation model execution. These stochastic processes are computed at discrete instant time events to represent the system state through the flow chart.\n", "\n", "### Agent based\n", "Agent based models are decentralised models, where each agent has individual behavioural rules that define how they interact with each other. This is a bottom-up approach where the system model is build from individual models of its inner components. \n", "\n", "## Montecarlo Simulation\n", "Montecarlo Simulation is probably the first simulation technique, and it basically consists on the computation of a mathematical model of the system which depends on random variables. The model is computed for a number of iterations (simulations) and the output is computed as the average. By the central limit theorem, this average will converge to a normal distribution, and therefore, it is possible to establish some confidence intervals to bound the simulation result.\n", "\n", "Thus, the steps in a Montecarlo simulation are:\n", "\n", "- **Define the output expression:** Define a mathematical model of the variable(s) that we want to simulate, the model implements random variables.\n", "\n", "- **Generate random values:** Generate values for the random input variables of the model. The generated values must be defined in a valid input domain using a probability distribution (e.g. normal or uniform distribution)\n", "\n", "- **Compute output:** Compute the value of the output expression for the random input values using the output expression. The output expression is computed a given number of iterations and the results are collected to get some statistics. The output of the simulation is the average value of the different iterations.\n", "\n", "- **Check confidence interval of solution:** Finally, we check the confidence intervals for a given confidence level and if the confidence interval is not satisfactory, the number of simulations should be increased.\n", "\n", "![montecarlo](img/montecarlo.png){width=50%}\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 0 }